/**
 * =========================================================================
 *  Copyright 2010 NCS Pte. Ltd. All Rights Reserved
 *
 *  This software is confidential and proprietary to NCS Pte. Ltd. You shall
 *  use this software only in accordance with the terms of the license
 *  agreement you entered into with NCS.  No aspect or part or all of this
 *  software may be reproduced, modified or disclosed without full and
 *  direct written authorisation from NCS.
 *
 *  NCS SUPPLIES THIS SOFTWARE ON AN "AS IS" BASIS. NCS MAKES NO
 *  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
 *  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
 *  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 *  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 *  Change Revision
 * -------------------------------------------------------------------------
 *  Version    Remarks
 * =========================================================================
 */
package canglang.scan.user.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import canglang.scan.core.base.dao.BaseDAO;
import canglang.scan.core.base.pagination.PaginatedListHelper;
import canglang.scan.core.base.util.StringUtil;
import canglang.scan.user.model.UserModel;
import canglang.scan.user.to.SearchTO;

/**
 * 
 */
@SuppressWarnings("unchecked")
public class UserDAO extends BaseDAO {
    public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("userRole", StringUtil.convertEmpty2Null(searchTO.getUserRole()));
        return (Integer) getSqlMapClientTemplate().queryForObject("searchUserCount", map);
    }

    /**
     * call this method to get pagination record list
     * @param helper
     * @param oldSearchTO
     * @return
     */
    public List searchList(PaginatedListHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        String userId=StringUtil.convertEmpty2Null(searchTO.getUserId());
        if(userId!=null)userId="%"+userId+"%";
        String userName=StringUtil.convertEmpty2Null(searchTO.getUserName());
        if(userName!=null)userName="%"+userName+"%";
        map.put("userId", userId);
        map.put("userName", userName);
        map.put("userRole", StringUtil.convertEmpty2Null(searchTO.getUserRole()));
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchUserList", map);
    }
    
    public  UserModel addUser(UserModel userModel){
        String sql="addUser";
        Map map=objectToMap(userModel);
        return (UserModel)getSqlMapClientTemplate().insert(sql,map);
    }
    public UserModel getUser(String userId){
        String sql="getUser";
        Map map=new HashMap();
        map.put("userId", userId);
        return (UserModel)getSqlMapClientTemplate().queryForObject(sql,map);
    }
    
    public UserModel saveUser(UserModel userModel){
        String sql="saveUser";
        Map map=objectToMap(userModel);
        getSqlMapClientTemplate().update(sql,map);
        return this.getUser(userModel.getUserName());
    }
    
    public void deleteUser(String userId){
        String sql="deleteUser";
        Map map=new HashMap();
        map.put("userId", userId);
        getSqlMapClientTemplate().delete(sql,map);
    }     

}
